package Impl;

import Util.JDBC;
import dao.adminDao;
import entity.Admin;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class adminDaoImpl implements adminDao {
    Connection conn=null;
    PreparedStatement ps=null;
    ResultSet rs =null;
    public void commonUp(String sql,Object ...args){
        try {
            conn= JDBC.getConnection();
            ps = conn.prepareStatement(sql);
            for (int i = 0; i <args.length ; i++) {
                ps.setObject(i+1,args[i]);
            }
            ps.execute();
            System.out.println("操作完成");
        } catch (SQLException e) {
            System.out.println("操作失败");
            e.printStackTrace();
        }finally {
            //关闭资源
            JDBC.closeAll(conn,ps,null);
        }
    }

    @Override
    public Admin findAdminByName(String aname) {
        String sql = "select * from admin where aname='"+aname+"'";
        Admin admin = new Admin();
        try {
            conn= JDBC.getConnection();
            ps=conn.prepareStatement(sql);
            rs=ps.executeQuery();
            if (rs.next()){
                admin.setId(rs.getInt("id"));
                admin.setAname(rs.getString("aname"));
                admin.setApwd(rs.getString("apwd"));
                admin.setAemail(rs.getString("aemail"));
                admin.setAphone(rs.getString("aphone"));
                admin.setIdentity(rs.getString("identity"));
            }else{
                admin=null;
            }
            System.out.println("查找成功！");
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBC.closeAll(conn,ps,rs);
        }
        return admin;
    }

    @Override
    public void addNewAdmin(Admin admin) {
        String sql="insert into admin (aname,apwd,identity,aphone,aemail,aRegTime) values (?,?,?,?,?,?)";
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        Object[] param={admin.getAname(),admin.getApwd(),admin.getIdentity(),admin.getAphone(),admin.getAemail(),dateString};
        commonUp(sql,param);
        System.out.println("新增管理员成功！");
    }

    @Override
    public List<Admin> findAdminByPages(int pageNow) {
        int pageCount = 5;
        conn = JDBC.getConnection();
        List<Admin> adminList = new ArrayList<>();
        String sql = "select * from admin limit ?,?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setObject(1, (pageNow-1)*pageCount);
            ps.setObject(2, pageCount);
            rs = ps.executeQuery();
            while(rs.next()) {
                Admin tmp = new Admin();
                tmp.setId(rs.getInt("id"));
                tmp.setApwd(rs.getString("apwd"));
                tmp.setAname(rs.getString("aname"));
                tmp.setAemail(rs.getString("aemail"));
                tmp.setAphone(rs.getString("aphone"));
                tmp.setaRegTime(rs.getString("aRegTime"));
                tmp.setIdentity(rs.getString("identity"));
                adminList.add(tmp);
            }
            System.out.println("分页查询成功");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            JDBC.closeAll(conn,ps,rs);
        }
        return adminList;
    }

    public List<Admin> common_findAll(ResultSet rs) throws SQLException {
        List<Admin> admins = new ArrayList<>();
        Admin admin = new Admin();
        while (rs.next()){
            admin.setAname(rs.getString("aname"));
            admin.setApwd(rs.getString("apwd"));
            admin.setAemail(rs.getString("aemail"));
            admin.setAphone(rs.getString("aphone"));
            admin.setIdentity(rs.getString("identity"));
            admins.add(admin);
        }
        return admins;

    }


    @Override
    public List<Admin> findAllAdmin() {
        List<Admin> admins = new ArrayList<>();
        String sql = "select * from admin";
        try {
            conn= JDBC.getConnection();
            ps=conn.prepareStatement(sql);
            rs=ps.executeQuery();
            admins=common_findAll(rs);
            System.out.println("查找成功！");
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBC.closeAll(conn,ps,rs);
        }
        return admins;
    }

    @Override
    public void updateAdmin(Admin admin) {
        System.out.println("开始更新管理员信息");
        StringBuilder sql =new StringBuilder( "update admin set aname=?,apwd=?,aphone=?,aemail=?,aRegTime=?");
        ArrayList<Object> pList=new ArrayList<Object>();
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        pList.add(admin.getAname());
        pList.add(admin.getApwd());
        pList.add(admin.getAphone());
        pList.add(admin.getAemail());
        pList.add(dateString);
        if(admin.getIdentity()!=null){
            sql.append(",identity=?");
            pList.add(admin.getIdentity());
        }
        sql.append(" where id=?");
        pList.add(admin.getId());
        Object[] param=pList.toArray();
        commonUp(sql.toString(), param);
    }

    @Override
    public void deleteAdmin(int id) {
        System.out.println("正在执行删除管理员操作");
        String sql ="delete from admin where id=?";
        commonUp(sql,id);
    }

    @Override
    public List<Admin> findAdminByCondition(String condition) {
        String sql = "select * from admin where aphone=? or aemail=? or aname='"+condition+"'";
        conn = JDBC.getConnection();
        List<Admin> adminList = new ArrayList<>();
        try {
            ps = conn.prepareStatement(sql);
            ps.setObject(1,condition);
            ps.setObject(2,condition);
            rs = ps.executeQuery();
            adminList=common_findAll(rs);
            System.out.println("查询成功");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            JDBC.closeAll(conn,ps,rs);
        }
        return adminList;
    }

    @Override
    public Admin findAdminByID(int id) {
        String sql = "select * from admin where id="+id;
        Admin admin = new Admin();
        try {
            conn= JDBC.getConnection();
            ps=conn.prepareStatement(sql);
            rs=ps.executeQuery();
            if (rs.next()){
                admin.setId(rs.getInt("id"));
                admin.setAname(rs.getString("aname"));
                admin.setApwd(rs.getString("apwd"));
                admin.setAemail(rs.getString("aemail"));
                admin.setAphone(rs.getString("aphone"));
                admin.setIdentity(rs.getString("identity"));
            }else{
                admin=null;
                System.out.println("查找失败！");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBC.closeAll(conn,ps,rs);
        }
        return admin;
    }

    public static void main(String[] args) {
        adminDaoImpl adminDao = new adminDaoImpl();
        System.out.println(adminDao.findAdminByName("B夜沉渊").getId());
    }

}
